import requests
req = requests.session()

# import urllib3
# urllib3.disable_warnings()
# print(req.cookies.values())
# ,verify=False

httpStatus = {'404': '404：（未找到） 服务器找不到请求的网页。',
              '403': '403：（禁止） 服务器拒绝请求。',
              '400': '400：（错误请求） 服务器不理解请求的语法。',
              '408': '408：（请求超时） 服务器等候请求时发生超时。',
              '500': '500：（服务器内部错误） 服务器遇到错误，无法完成请求。',
              '503': '503：（服务不可用） 服务器目前无法使用（由于超载或停机维护）',
              '502': '502：（错误网关） 服务器作为网关或代理，从上游服务器收到无效响应。',
              '504': '504：（网关超时） 服务器作为网关或代理，但是没有及时从上游服务器收到请求。',
              '501': '501：（尚未实施） 服务器不具备完成请求的功能。 例如，服务器无法识别请求方法时可能会返回此代码。',
              '505': '505：（HTTP 版本不受支持） 服务器不支持请求中所用的 HTTP 协议版本。',
              '401': '401：（未授权） 请求要求身份验证。 对于需要登录的网页，服务器可能返回此响应。',
              '405': '405：（方法禁用） 禁用请求中指定的方法。',
              '406': '406：（不接受） 无法使用请求的内容特性响应请求的网页。',
              '407': '407：（需要代理授权） 此状态代码与 401（未授权）类似，但指定请求者应当授权使用代理。',
              }

class Request(object):

    def __init__(self, url_root):
        self.url_root = url_root

    def get_request(self, tempURL, data, header):
        url = self.url_root + tempURL

        try:
            if data == '':
                response = req.get(url=url, headers=header)
            else:
                response = req.get(url=url, params=data, headers=header)

        except requests.RequestException as e:
            print('%s%s' % ('RequestException url: ', url))
            print(e)
            return ()

        except Exception as e:
            print('%s%s' % ('Exception url: ', url))
            print(e)
            return ()

        response_dicts = dict()
        response_dicts['code'] = response.status_code
        try:
            response_dicts['body'] = response.json()
        except Exception as e:
            response_dicts['body'] = httpStatus[str(response.status_code)]
        response_dicts['text'] = response.text
        response_dicts['request_url'] = response.request.url
        # print("request_url为"+str(response_dicts['request_url']))
        response_dicts['request_header'] = response.request.headers
        # print("request_header为" + str(response_dicts['request_header']))
        response_dicts['request_data'] = response.request.body
        # print("request_data为" + str(response_dicts['request_data']))
        return response_dicts

    def post_request(self, tempURL, data, header):
        url = self.url_root + tempURL
        try:
            if data is None:
                response = req.post(url=url, headers=header)
            else:
                response = req.post(url=url, data=data, headers=header)
        except requests.RequestException as e:
            print('%s%s' % ('RequestException url: ', url))
            print(e)
            return ()

        except Exception as e:
            print('%s%s' % ('Exception url: ', url))
            print(e)
            return ()

        response_dicts = dict()
        response_dicts['code'] = response.status_code
        try:
            response_dicts['body'] = response.json()
        except Exception as e:
            response_dicts['body'] = httpStatus[str(response.status_code)]
        response_dicts['text'] = response.text
        response_dicts['request_url'] = url
        response_dicts['request_header'] = response.request.headers
        response_dicts['request_data'] = response.request.body
        # print("request_data为" + str(response_dicts['request_data']))
        return response_dicts


    def put_request(self, tempURL, data, header):
        url = self.url_root + tempURL
        try:
            if data is None:
                response = req.put(url=url, headers=header)
            else:
                response = req.put(url=url, data=data, headers=header)
        except requests.RequestException as e:
            print('%s%s' % ('RequestException url: ', url))
            print(e)
            return ()

        except Exception as e:
            print('%s%s' % ('Exception url: ', url))
            print(e)
            return ()

        response_dicts = dict()
        response_dicts['code'] = response.status_code
        try:
            response_dicts['body'] = response.json()
        except Exception as e:
            print(e)
            response_dicts['body'] = httpStatus[str(response.status_code)]
        response_dicts['text'] = response.text
        response_dicts['request_url'] = url
        response_dicts['request_header'] = response.request.headers
        response_dicts['request_data'] = response.request.body
        return response_dicts

    def delete_request(self, tempURL, data, header):
        url = self.url_root + tempURL

        try:
            if data == '':
                response = req.delete(url=url, headers=header)
            else:
                response = req.delete(url=url, params=data, headers=header)

        except requests.RequestException as e:
            print('%s%s' % ('RequestException url: ', url))
            print(e)
            return ()

        except Exception as e:
            print('%s%s' % ('Exception url: ', url))
            print(e)
            return ()

        response_dicts = dict()
        response_dicts['code'] = response.status_code
        try:
            response_dicts['body'] = response.json()
        except Exception as e:
            response_dicts['body'] = httpStatus[str(response.status_code)]
        response_dicts['text'] = response.text
        response_dicts['request_url'] = response.request.url
        response_dicts['request_header'] = response.request.headers
        response_dicts['request_data'] = response.request.body
        return response_dicts